package com.common.handler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.common.utils.BeanUtils;

/**
 * PCL参数处理器-针对params中的某个key下的List&lt;T&gt;做处理
 * @author iuie
 * @param <T>
 */
public abstract class PCLAbstractListParamHandler<T> extends PCLAbstractParamHandler {
	
	protected List<T> listParam;
	
	public PCLAbstractListParamHandler(String key) {
		super(key);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> handle(Map<String, Object> params) {
		if (params == null || this.key == null || params.get(this.key) == null) {
			return params;
		}
		this.listParam = ((List<T>) params.get(this.key));
		if (this.listParam.size() <= 0) {
			return params;
		}
		
		List<Map<String, Object>> list = new ArrayList<>();
		
		Iterator<T> it = this.listParam.iterator();
		while(it.hasNext()) {
			T obj = it.next();
			Map<String, Object> objMap = BeanUtils.beanToMap(obj);
			Map<String, Object> newObjMap = handleObjectMap(objMap);
			list.add(newObjMap);
		}
		params.put(this.key, list);
		return params;
	}
	
	/**
	 * 处理params&lt;key&gt;中List对象的单个元素转成的Map&lt;String,Object&gt objMap;
	 * @param objMap
	 * @return
	 * @author iuie
	 * @date 2017年7月25日 下午2:22:32
	 */
	public abstract Map<String, Object> handleObjectMap(Map<String, Object> objMap);

}
